<?php
/**
 * 上传文件临时表 - RPC
 * 
 * @author karl <karl.tang@aiyuangong.com>
 */
namespace app\seller\rpc;

use think\Validate;

class Upload extends \app\common\rpc\Rpc
{

    /**
     * 添加upload临时表记录
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function insert($data)
    {
        $rule = [
            "name|图片" => "max:100",
            "size|图片大小" => "number",
            "type|类型" => "number",
            "upload_time|上传时间" => "number",
            "item_id|业务ID" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $result = \think\Db::name('upload')->insert($data);
            return [
                'result' => $result
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 更新upload临时表记录
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function update($where, $data)
    {
        $rule = [
            "name|图片" => "max:100",
            "size|图片大小" => "number",
            "type|类型" => "number",
            "upload_time|上传时间" => "number",
            "item_id|业务ID" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $result = \think\Db::name('upload')->where($where)->update($data);
            return [
                'result' => $result
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 删除upload临时表记录
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function delete($where)
    {
        try {
            $result = \think\Db::name('upload')->where($where)->delete();
            return [
                'result' => $result
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }
}
